package com.fenxiangle.yueding.feature.focus.view;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.entity.bo.TranscctionBo;
import com.fenxiangle.yueding.feature.home.view.adapter.ExpandableItemAdapter;
import com.fenxiangle.yueding.framework.api.UserApi;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class WalletDetailActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.wallet_recyclerView)
    RecyclerView walletRecyclerView;
    ExpandableItemAdapter adapter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_wallet_detail;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initView() {
        initToolBar(toolbar, "明细", true, true);
        walletRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void initData() {

        AM.api().createApiService(UserApi.class)
                .getTransaction()
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<List<TranscctionBo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        showLoading(d);
                    }

                    @Override
                    public void onNext(List<TranscctionBo> walletFirstBos) {
                        dismissLoading();
                        ArrayList<MultiItemEntity> res = new ArrayList<>();

                        if (walletFirstBos != null && !walletFirstBos.isEmpty()) {
                            for (int i = 0; i < walletFirstBos.size(); i++) {
                                TranscctionBo lv0 = walletFirstBos.get(i);
                                for (int j = 0; j < lv0.getCommissionFormMap().size(); j++) {
                                    TranscctionBo.CommissionFormMapBean lv1 = lv0.getCommissionFormMap().get(j);
                                    lv0.addSubItem(lv1);
                                }
                                res.add(lv0);
                            }
                        }

                        adapter = new ExpandableItemAdapter(res);
                        walletRecyclerView.setAdapter(adapter);
                        adapter.expandAll();
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        dismissLoading();
                    }
                });

    }


}
